<?php  
class ControllerArticleArticle extends Controller {  
	private $error = array();
   
  	public function index() {
     	$this->document->setTitle('文章列表');
		$this->load->model('article/article');
    	$this->getList(); 
  	}
   
  	public function insert() {
        
    	$this->document->setTitle('添加文章');
		
		$this->load->model('article/article');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_article_article->addArticle($this->request->post);
			
			$this->session->data['success'] =  '添加文章成功';;
			
			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
						
			$this->redirect($this->url->link('article/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    	}
	
    	$this->getForm();
  	}

  	public function update() {

    	$this->document->setTitle('编辑文章');
		
		$this->load->model('article/article');

    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_article_article->editArticle($this->request->get['article_id'], $this->request->post);
			
			$this->session->data['success'] = '编辑文章成功';
			
			$url = '';
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('article/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    	}
	
    	$this->getForm();
  	}
 
  	public function delete() { 
    	$this->document->setTitle('删除文章');
		
		$this->load->model('article/article');
		
    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
      		foreach ($this->request->post['selected'] as $article_id) {
				$this->model_article_article->deleteArticle($article_id);	
			}

			$this->session->data['success'] = '删除文章成功';
			
			$url = '';
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('article/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    	}
	
    	$this->getList();
  	}

  	private function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'title';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
			
		$url = '';
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
					
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => '首页',
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => '文章管理',
			'href'      => $this->url->link('articlearticle', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
			
		$this->data['insert'] = $this->url->link('article/article/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['delete'] = $this->url->link('article/article/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');			
			
    	$this->data['articles'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		$article_total = $this->model_article_article->getTotalArticles();
		$results = $this->model_article_article->getArticles($data);
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => '编辑',
				'href' => $this->url->link('article/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $result['article_id'] . $url, 'SSL')
			);
					
      		$this->data['articles'][] = array(
				'article_id'    => $result['article_id'],
				'title'    => $result['title'],
				'content'    => $result['content'],
				'sort_order'  => $result['sort_order'],
				'category_id'   => $result['category_id'],
				'status'     => ($result['status'] ? '允许' : '禁止'),
				'categoryname'     =>  $result['categoryname'],
				'date_added' => date('d/m/Y', strtotime($result['date_added'])),
				'selected'   => isset($this->request->post['selected']) && in_array($result['article_id'], $this->request->post['selected']),
				'action'     => $action
			);
		}	
		$this->data['heading_title'] = '文章管理';
		
		$this->data['text_no_results'] = '没有记录';

		$this->data['column_title'] = '标题';
		$this->data['column_sort_order'] = '排序';
		$this->data['column_category_name'] = '目录名称';
		$this->data['column_status'] = '状态';
		$this->data['column_date_added'] = '添加日期';
		$this->data['column_action'] = '动作';
		
		$this->data['button_insert'] = '添加';
		$this->data['button_delete'] = '删除';
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
					
		$this->data['sort_title'] = $this->url->link('article/article', 'token=' . $this->session->data['token'] . '&sort=title' . $url, 'SSL');
		$this->data['sort_status'] = $this->url->link('article/article', 'token=' . $this->session->data['token'] . '&sort=status' . $url, 'SSL');
		$this->data['sort_date_added'] = $this->url->link('article/article', 'token=' . $this->session->data['token'] . '&sort=date_added' . $url, 'SSL');
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $article_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = '当前第 {start} 到 {end} 条 共 {total} 条 (共 {pages} 页)';
		$pagination->url = $this->url->link('article/article', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
								
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'article/article_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
  	}
	
	private function getForm() {
    	$this->data['heading_title'] = '文章管理';

    	$this->data['text_enabled'] = '允许';
    	$this->data['text_disabled'] = '禁止';
    	$this->data['text_select'] = '请选择';
		
    	$this->data['entry_title'] = '标题';
    	$this->data['entry_category'] = '目录';
    	$this->data['entry_image'] = '图片';
    	$this->data['entry_content'] = '内容';
    	$this->data['entry_sort_order'] = '排序';
    	$this->data['entry_status'] = '状态';
    	$this->data['entry_top'] = '置顶';
    	$this->data['entry_hot'] = '热门';
    	$this->data['entry_meta_keyword'] = 'Meta关键词';

    	$this->data['button_save'] = '保存';
    	$this->data['button_cancel'] = '取消';
    

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		} 		
        if (isset($this->error['category'])) {
			$this->data['error_category'] = $this->error['category'];
		} else {
			$this->data['error_category'] = '';
		}
        
		$url = '';
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => '首页',
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => '文章管理',
			'href'      => $this->url->link('article/article', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
		
		if (!isset($this->request->get['article_id'])) {
			$this->data['action'] = $this->url->link('article/article/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('article/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $this->request->get['article_id'] . $url, 'SSL');
		}
		  
    	$this->data['cancel'] = $this->url->link('article/article', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['token'] = $this->session->data['token'];

    	if (isset($this->request->get['article_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$article_info = $this->model_article_article->getArticle($this->request->get['article_id']);
    	}
    	
        if (isset($this->request->post['title'])) {
      		$this->data['title'] = $this->request->post['title'];
    	} elseif (isset($article_info)) {
			$this->data['title'] = $article_info['title'];
		} else {
      		$this->data['title'] = '';
    	}        
        
        if (isset($this->request->post['category_id'])) {
      		$this->data['category_id'] = $this->request->post['category_id'];
    	} elseif (isset($article_info)) {
			$this->data['category_id'] = $article_info['category_id'];
		} else {
      		$this->data['category_id'] = '';
    	}
  
		if (isset($this->request->post['article_description'])) {
			$this->data['article_description'] = $this->request->post['article_description'];
		} elseif (isset($article_info)) {
			$this->data['article_description'] = $this->model_article_article->getArticleDescriptions($this->request->get['article_id']);
		} else {
			$this->data['article_description'] = array();
		}   	
 
     	if (isset($this->request->post['status'])) {
      		$this->data['status'] = $this->request->post['status'];
    	} elseif (isset($article_info)) {
			$this->data['status'] = $article_info['status'];
		} else {
      		$this->data['status'] = 0;
    	}
        
        
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (isset($article_info)) {
			$this->data['image'] = $article_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('tool/image');

		if (isset($article_info) && $article_info['image'] && file_exists(DIR_IMAGE . $article_info['image'])) {
			$this->data['preview'] = $this->model_tool_image->resize($article_info['image'], 100, 100);
		} else {
			$this->data['preview'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
        
        $this->load->model('article/category');
        $categories = $this->model_article_category->getCategories(0);
        $this->data['categories'] = $categories;
		
        
        if (isset($this->request->post['top'])) {
			$this->data['top'] = $this->request->post['top'];
		} elseif (isset($article_info)) {
			$this->data['top'] = $article_info['top'];
		} else {
			$this->data['top'] = 0;
		}

        if (isset($this->request->post['hot'])) {
			$this->data['hot'] = $this->request->post['hot'];
		} elseif (isset($article_info)) {
			$this->data['hot'] = $article_info['hot'];
		} else {
			$this->data['hot'] = 0;
		}
        
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} elseif (isset($article_info)) {
			$this->data['sort_order'] = $article_info['sort_order'];
		} else {
			$this->data['sort_order'] = 0;
		}
		
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (isset($article_info)) {
			$this->data['status'] = $article_info['status'];
		} else {
			$this->data['status'] = 1;
		}
		$this->template = 'article/article_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());	
  	}
  	
  	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'article/article')) {
      		$this->error['warning'] = '没有权限';
    	}
    
    	if ((strlen(utf8_decode($this->request->post['title'])) < 3) || (strlen(utf8_decode($this->request->post['title'])) > 20)) {
      		$this->error['title'] = '标题长度为3-20个字符';
    	}    	
        if (empty($this->request->post['category_id'])) {
      		$this->error['category'] = '必须选择目录';
    	}
	
    	if (!$this->error) {
      		return true;
    	} else {
      		return false;
    	}
  	}

  	private function validateDelete() { 
    	if (!$this->user->hasPermission('modify', 'article/article')) {
      		$this->error['warning'] = '没有权限';
    	}
		 
		if (!$this->error) {
	  		return true;
		} else { 
	  		return false;
		}
  	}
}
?>